package com.rong.action;
import java.util.List;
import java.util.Map;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.rong.DAO.ZynCarDAO;
import com.rong.DAO.ZynCompanyDAO;
import com.rong.DAO.ZynPrice1DAO;
import com.rong.ORM.ZynCar;
import com.rong.ORM.ZynCompany;
import com.rong.ORM.ZynPrice1;
import com.rong.ORM.ZynQueryPrice;
import com.rong.ORM.ZynUser;
import com.rong.hibernate.Const;

public class Price1Action extends ActionSupport {
	ZynPrice1 price1=new ZynPrice1();
	ZynPrice1DAO price1Dao=new ZynPrice1DAO();
	private List<ZynPrice1> list;
	private String saveFlag="";
	private int id=-1; // the Price1 id indatabase.
	private List<ZynCompany> comlist;
	private List<ZynCar> carlist;
	ZynCompany com=new ZynCompany();
	ZynCompanyDAO comDao=new ZynCompanyDAO();
	ZynCar car=new ZynCar();
	ZynCarDAO carDao=new ZynCarDAO();


	private int rowsPerPage =25;// 每页显示几条
	private int page = 1; // 默认当前页
	private int totalPage;// 总共多少页
	private int planNum;// 总过多少条
	
	ZynQueryPrice queryCrt=new ZynQueryPrice();
	
	/**
	 * @return the comlist
	 */
	public List<ZynCompany> getComlist() {
		return comlist;
	}
	/**
	 * @param comlist the comlist to set
	 */
	public void setComlist(List<ZynCompany> comlist) {
		this.comlist = comlist;
	}
	/**
	 * @return the carlist
	 */
	public List<ZynCar> getCarlist() {
		return carlist;
	}
	/**
	 * @param carlist the carlist to set
	 */
	public void setCarlist(List<ZynCar> carlist) {
		this.carlist = carlist;
	}
	/**
	 * @return the price1
	 */
	public ZynPrice1 getPrice1() {
		return price1;
	}
	/**
	 * @param price1 the price1 to set
	 */
	public void setPrice1(ZynPrice1 price1) {
		this.price1 = price1;
	}
	/**
	 * @return the list
	 */
	public List<ZynPrice1> getList() {
		return list;
	}
	/**
	 * @param list the list to set
	 */
	public void setList(List<ZynPrice1> list) {
		this.list = list;
	}
	/**
	 * @return the saveFlag
	 */
	public String getSaveFlag() {
		return saveFlag;
	}
	/**
	 * @param saveFlag the saveFlag to set
	 */
	public void setSaveFlag(String saveFlag) {
		this.saveFlag = saveFlag;
	}
	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the com
	 */
	public ZynCompany getCom() {
		return com;
	}
	/**
	 * @param com the com to set
	 */
	public void setCom(ZynCompany com) {
		this.com = com;
	}
	/**
	 * @return the car
	 */
	public ZynCar getCar() {
		return car;
	}
	/**
	 * @param car the car to set
	 */
	public void setCar(ZynCar car) {
		this.car = car;
	}
	/**
	 * @return the rowsPerPage
	 */
	public int getRowsPerPage() {
		return rowsPerPage;
	}
	/**
	 * @param rowsPerPage the rowsPerPage to set
	 */
	public void setRowsPerPage(int rowsPerPage) {
		this.rowsPerPage = rowsPerPage;
	}
	/**
	 * @return the page
	 */
	public int getPage() {
		return page;
	}
	/**
	 * @param page the page to set
	 */
	public void setPage(int page) {
		this.page = page;
	}
	/**
	 * @return the totalPage
	 */
	public int getTotalPage() {
		return totalPage;
	}
	/**
	 * @param totalPage the totalPage to set
	 */
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	/**
	 * @return the planNum
	 */
	public int getPlanNum() {
		return planNum;
	}
	/**
	 * @param planNum the planNum to set
	 */
	public void setPlanNum(int planNum) {
		this.planNum = planNum;
	}
	/**
	 * @return the queryCrt
	 */
	public ZynQueryPrice getQueryCrt() {
		return queryCrt;
	}
	/**
	 * @param queryCrt the queryCrt to set
	 */
	public void setQueryCrt(ZynQueryPrice queryCrt) {
		this.queryCrt = queryCrt;
	}
	@Override
    public String execute() throws Exception {
        this.setSaveFlag("");
        
        //价格所属分公司
        ActionContext actionContext = ActionContext.getContext();         
        Map session = actionContext.getSession(); 
        ZynUser loguser=(ZynUser) session.get("loginUser");    // the user session
        this.price1.setExtattr2(String.valueOf(loguser.getComId()));
        
        //价格所属分公司
		price1Dao.save(this.price1);        //保存注册的price1对象
		 this.setSaveFlag("保存成功..");
        return SUCCESS;
    }
	 public String findall() throws Exception {
	        
		 ActionContext actionContext = ActionContext.getContext();         
         Map session = actionContext.getSession(); 
         ZynUser loguser=(ZynUser) session.get("loginUser");    // the user session
         if(loguser.getZyncom().getComProperty().equalsIgnoreCase(Const.comTypetop)){//公司本部可以查看所有
        	 this.setList(price1Dao.findAll());        //所有单价列表
         }else{
        	 this.setList(price1Dao.findByExtattr2(String.valueOf(loguser.getComId())));        //所有分公司单价列表
         }
	        return "success_list";
	    }
	 public String findbyid() throws Exception {
	        
		this.setPrice1(price1Dao.findById(this.getId()));// find the price1 by id	
		this.setCarlist(carDao.findAll());        //保存注册的user对象
		 this.setComlist(comDao.findAll());        //保存注册的user对象
	        return "s_find_one";
	       
	    }
	 public String edit() throws Exception {
	      			
		   price1Dao.merge(this.getPrice1());// edit the price1 by id			
		    return "s_edit_one";
		    }
	 public String delete() throws Exception {
	        
			price1Dao.delete(price1Dao.findById(this.getId()));// delete the price1 by id			
		    return "s_delete_one";
		    }
	 public String toGetPriceList() throws Exception {
		  
			this.setList(price1Dao.findPriceQueryByPage(page, rowsPerPage,this.queryCrt));       
	        this.setPlanNum(price1Dao.priceTatolRows(this.queryCrt).size());
	        this.setTotalPage(this.getPlanNum() % this.getRowsPerPage()==0?this.getPlanNum()/this.getRowsPerPage():this.getPlanNum()/this.getRowsPerPage()+1);     
		        return "success_list";
		    }
		 
	
}
